<?php
/**
 * Created by PhpStorm.
 * User: 14775
 * Date: 2017/5/26
 * Time: 11:17
 */

namespace Common\Lib;


use Think\Cache\Driver\Redis;

class RedisHandle
{

    const PREFIX_TOKEN = 'TOKEN_';
    const PREFIX_TOKEN_APPID = 'TOKEN_APPID_';

    const PREFIX_SMS = 'SMS_';//手机短信验证码前缀
    const PREFIX_WX_ACCESSTOKEN = 'WX_ACCESSTOKEN';//微信accessToken前缀
    const PREFIX_WX_JSAPI_TICKET = 'PREFIX_WX_JSAPI_TICKET'; //微信js_ticket前缀

    const PREFIX_MEMBER = 'MEMBER_';//用户信息前缀
    const PREFIX_WEIXIN_USER = 'WEIXIN_USER_';//微信用户信息

    const PREFIX_SMS_ACCESSTOKEN = 'SMS_ACCESSTOKEN';//手机短信平台token


    static public $redis;

    public static function saveSMSAccesstoken($access_token)
    {
        self::$redis->set(self::PREFIX_SMS_ACCESSTOKEN, $access_token);
    }

    public static function getSMSAccesstoken()
    {
        $vcode = self::$redis->get(self::PREFIX_SMS_ACCESSTOKEN);
        return $vcode;
    }


    /**
     * 保存手机验证码
     * @param $mobile
     * @param $vcode
     * @return bool
     */
    public static function saveSMSVCode($mobile, $vcode)
    {

        return self::$redis->set(self::PREFIX_SMS . $mobile, $vcode, 300);
    }

    /**
     * 获取手机验证码
     * @param $mobile
     * @return bool
     */
    public static function getSMSVCode($mobile)
    {

        $vcode = self::$redis->get(self::PREFIX_SMS . $mobile);
        return $vcode;
    }


    //===============微信accessToken缓存操作=================>>>
    public static function saveWxAccessToken($value, $expired)
    {
        $result = self::$redis->set(self::PREFIX_WX_ACCESSTOKEN, $value, $expired);
        return $result;
    }

    public static function getWxAccessToken()
    {
        $result = self::$redis->get(self::PREFIX_WX_ACCESSTOKEN);
        return $result;
    }

    public static function removeWxAccessToken()
    {
        return self::$redis->rm(self::PREFIX_WX_ACCESSTOKEN);
    }


    public static function saveWxJSTicket($value, $expired)
    {
        return self::$redis->set(self::PREFIX_WX_JSAPI_TICKET, $value, $expired);
    }

    public static function getWxJSTicket()
    {
        return self::$redis->get(self::PREFIX_WX_JSAPI_TICKET);
    }

    public static function removeWxJSTicket()
    {
        return self::$redis->rm(self::PREFIX_WX_JSAPI_TICKET);
    }





    //===============微信accessToken缓存操作=================<<<

    //===============用户信息缓存操作=================>>>
    public static function setMember($cachename, $value, $expired = 86400)
    {
        return self::$redis->set(self::PREFIX_MEMBER . $cachename, $value, $expired);
    }

    public static function getMember($cachename)
    {
        if ($cachename) {

            return self::$redis->get(self::PREFIX_MEMBER . $cachename);
        }
    }

    public static function removeMember($cachename)
    {
        if ($cachename) {
            return self::$redis->rm(self::PREFIX_MEMBER . $cachename);
        }
    }

    //===============用户信息缓存操作=================<<<


    public static function setWeixinUser($cachename, $value, $expired = 86400)
    {
        return self:: $redis->set(self::PREFIX_WEIXIN_USER . $cachename, $value, $expired);
    }

    public static function getWeixinUser($cachename)
    {
        if ($cachename) {
            return self::$redis->get(self::PREFIX_WEIXIN_USER . $cachename);
        }
    }
}

RedisHandle::$redis = new Redis();